package com.lecture.java8.zip;

import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2020/7/23 14:21
 */
public class CompressDirectory2 {
    /**
     * @author luyn
     * @description
     * @date 14:21 2020/7/23
     * @param file 递归的开始文件夹
     * @param list 存放递归文件夹下所有文件的绝对路径
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getFileDirectory(File file, List<String> list) {
        if (file.isFile()) {
            list.add(file.getAbsolutePath());
        } else {
            File[] files = file.listFiles();
            if (files == null) {
                return list;
            }
            for (File value : files) {
                CompressDirectory2.getFileDirectory(value, list);
            }
        }
        return list;
    }

    /**
     * @author luyn
     * @description
     * @date 14:30 2020/7/23
     * @param filePath 需要压缩的目录
     * @param zipFilePath 压缩后的zip绝对路径
     * @param isFile 是否是文件 true是文件
     * @return int
     */
    public static int compress(String filePath, String zipFilePath, boolean isFile) throws IOException {
        byte[] buf = new byte[1024];
        File zipFile = new File(zipFilePath);
        int fileCount = 0;//记录压缩了几个文件？
        try {

            List<String> filePaths = new ArrayList<>();
            filePaths = CompressDirectory2.getFileDirectory(new File(filePath), filePaths);

            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
            for (String relativePath : filePaths) {
                System.out.println("开始压缩：" + fileCount);
                if (StringUtils.isEmpty(relativePath)) {
                    continue;
                }
                File sourceFile = new File(relativePath);//绝对路径找到file
                if (!sourceFile.exists()) {
                    continue;
                }

                FileInputStream fis = new FileInputStream(sourceFile);
                // 保持文件目录结构 相对结构路径
                if (isFile) {
                    zos.putNextEntry(new ZipEntry(sourceFile.getName()));
                } else {
                    zos.putNextEntry(new ZipEntry(relativePath.replace(filePath, "")));
                }
                int len;
                while ((len = fis.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                fis.close();
                fileCount++;
            }
            zos.close();
            System.out.println("压缩完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileCount;
    }

    public static void main(String[] args) throws IOException {
        String filePath = "G:\\bilibili\\apache-tomcat-7.0.78";

        String filePathZip = "F:\\Microservices\\test.zip";

        int compress = CompressDirectory2.compress(filePath, filePathZip, false);
        System.out.println("共压缩了：" + compress + "个文件！");
    }
}
